/*
 * Copyright (C) 2014 Stefan Schweitzer
 *
 * This software was created by Stefan Schweitzer as a student's project at
 * Fachhochschule Kaiserslautern (University of Applied Sciences).
 * Supervisor: Professor Dr. Thomas Allweyer. For more information please see
 * http://www.fh-kl.de/~allweyer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this Software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.bpmn_simulator.animation.input;

import java.util.HashSet;
import java.util.Set;

import com.googlecode.bpmn_simulator.animation.element.visual.Diagram;

public abstract class AbstractDefinition<E extends Diagram<?>>
		implements Definition<E> {

	private final Set<DefinitionListener> definitionListeners
			= new HashSet<DefinitionListener>();

	@Override
	public void addDefinitionListener(final DefinitionListener listener) {
		synchronized (definitionListeners) {
			definitionListeners.add(listener);
		}
	}

	@Override
	public void removeDefinitionListener(final DefinitionListener listener) {
		synchronized (definitionListeners) {
			assert definitionListeners.contains(listener);
			definitionListeners.remove(listener);
		}
	}

	protected void notifyError(final String message, final Throwable exception) {
		synchronized (definitionListeners) {
			for (final DefinitionListener listener : definitionListeners) {
				listener.error(message, exception);
			}
		}
	}

	protected void notifyWarning(final String message) {
		synchronized (definitionListeners) {
			for (final DefinitionListener listener : definitionListeners) {
				listener.warning(message);
			}
		}
	}

}
